package com.kexilo.system.system.service;

import com.kexilo.dict.utils.DictUtils;
import com.kexilo.core.system.domain.SysDictData;
import com.kexilo.system.system.mapper.SysDictDataMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 字典 业务层处理
 * 
 * @author Kexilo
 */
@Service
public class SysDictDataServiceImpl implements ISysDictDataService {
    
    private static final Logger log = LoggerFactory.getLogger(SysDictDataServiceImpl.class);
    
    @Autowired
    private SysDictDataMapper dictDataMapper;

    /**
     * 根据条件分页查询字典数据
     * 
     * @param dictData 字典数据信息
     * @return 字典数据集合信息
     */
    @Override
    public List<SysDictData> selectDictDataList(SysDictData dictData) {
        return dictDataMapper.selectDictDataList(dictData);
    }

    /**
     * 根据字典类型查询字典数据
     * 
     * @param dictType 字典类型
     * @return 字典数据集合信息
     */
    @Override
    @Cacheable(value = "dict:data", key = "#dictType", condition = "#dictType != null")
    public List<SysDictData> selectDictDataByType(String dictType) {
        return dictDataMapper.selectDictDataByType(dictType);
    }

    /**
     * 根据字典类型和字典键值查询字典数据信息
     * 
     * @param dictType 字典类型
     * @param dictValue 字典键值
     * @return 字典标签
     */
    @Override
    public String selectDictLabel(String dictType, String dictValue) {
        return dictDataMapper.selectDictLabel(dictType, dictValue);
    }

    /**
     * 根据字典数据ID查询信息
     * 
     * @param dictCode 字典数据ID
     * @return 字典数据
     */
    @Override
    @Cacheable(value = "dict:data:info", key = "#dictCode", condition = "#dictCode != null && #dictCode > 0")
    public SysDictData selectDictDataById(Long dictCode) {
        return dictDataMapper.selectDictDataById(dictCode);
    }

    /**
     * 批量删除字典数据信息
     * 
     * @param dictCodes 需要删除的字典数据ID
     */
    @Override
    @CacheEvict(value = {"dict:data", "dict:data:info"}, allEntries = true)
    public void deleteDictDataByIds(Long[] dictCodes) {
        for (Long dictCode : dictCodes) {
            SysDictData data = selectDictDataById(dictCode);
            dictDataMapper.deleteDictDataById(dictCode);
            // 缓存清理由适配器层或缓存注解处理
        }
    }

    /**
     * 新增保存字典数据信息
     * 
     * @param data 字典数据信息
     * @return 结果
     */
    @Override
    @CacheEvict(value = {"dict:data", "dict:data:info"}, allEntries = true)
    public int insertDictData(SysDictData data) {
        int row = dictDataMapper.insertDictData(data);
        // 缓存更新由适配器层或缓存注解处理
        return row;
    }

    /**
     * 修改保存字典数据信息
     * 
     * @param data 字典数据信息
     * @return 结果
     */
    @Override
    @CacheEvict(value = {"dict:data", "dict:data:info"}, allEntries = true)
    public int updateDictData(SysDictData data) {
        int row = dictDataMapper.updateDictData(data);
        // 缓存更新将由适配器层处理
        return row;
    }

    /**
     * 根据字典类型和字典标签查询字典数据信息
     * 
     * @param dictType 字典类型  
     * @param dictLabel 字典标签
     * @return 字典键值
     */
    @Override
    public String selectDictValue(String dictType, String dictLabel) {
        return dictDataMapper.selectDictValue(dictType, dictLabel);
    }
}
